package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.OrgStructure;
import com.mohe.dangjian.service.interfaces.IOrgStructure;
import com.mohe.dangjian.utils.Page;

public class OrgStructureImpl implements IOrgStructure {
	
	private IDao<OrgStructure> orgStructureDao;

	public IDao<OrgStructure> getOrgStructureDao() {
		return orgStructureDao;
	}

	public void setOrgStructureDao(IDao<OrgStructure> orgStructureDao) {
		this.orgStructureDao = orgStructureDao;
	}

	@Override
	public List<OrgStructure> getOrgStructureList(OrgStructure orgStructure,
			Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from OrgStructure where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 总支
		if (!StringUtils.isBlank(orgStructure.getGeneralBranch())) {
			where.append(" and generalBranch like ?");
			params.add("%%" + orgStructure.getGeneralBranch() + "%%");
		}
		// 分支
		if (!StringUtils.isBlank(orgStructure.getBranch())) {
			where.append(" and branch like ?");
			params.add("%%" + orgStructure.getBranch() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(orgStructure.getPartyType())) {
			where.append(" and partyType like ?");
			params.add("%%" + orgStructure.getPartyType() + "%%");
		}		
		
		where.append(" order by createDate desc");
		List<OrgStructure> list = orgStructureDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(OrgStructure orgStructure) {
		StringBuffer where = new StringBuffer();
		where.append("from OrgStructure where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 总支
		if (!StringUtils.isBlank(orgStructure.getGeneralBranch())) {
			where.append(" and generalBranch like ?");
			params.add("%%" + orgStructure.getGeneralBranch() + "%%");
		}
		// 分支
		if (!StringUtils.isBlank(orgStructure.getBranch())) {
			where.append(" and branch like ?");
			params.add("%%" + orgStructure.getBranch() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(orgStructure.getPartyType())) {
			where.append(" and partyType like ?");
			params.add("%%" + orgStructure.getPartyType() + "%%");
		}	
		
		return orgStructureDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveOrgStructure(OrgStructure orgStructure) {
		return orgStructureDao.insert(orgStructure);
	}

	@Override
	public boolean saveOrgStructureList(List<OrgStructure> orgStructureList) {
		return orgStructureDao.insert(orgStructureList);
	}

	@Override
	public OrgStructure getOrgStructureById(Long timeKey) {
		return orgStructureDao.getByKey(timeKey);
	}

	@Override
	public boolean updateOrgStructure(OrgStructure orgStructure) {
		return orgStructureDao.update(orgStructure);
	}

	@Override
	public boolean delOrgStructure(Long timeKey) {
		return orgStructureDao.delete(timeKey);
	}

	@Override
	public List<OrgStructure> getAllOrgStructure() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return orgStructureDao.query(where.toString(), null);
	}

	@Override
	public List<OrgStructure> getOrgStructureList(OrgStructure orgStructure) {
		StringBuffer where = new StringBuffer();
		where.append("from OrgStructure where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 总支
		if (!StringUtils.isBlank(orgStructure.getGeneralBranch())) {
			where.append(" and generalBranch like ?");
			params.add("%%" + orgStructure.getGeneralBranch() + "%%");
		}
		// 分支
		if (!StringUtils.isBlank(orgStructure.getBranch())) {
			where.append(" and branch like ?");
			params.add("%%" + orgStructure.getBranch() + "%%");
		}
		// 党组织类型
		if (!StringUtils.isBlank(orgStructure.getPartyType())) {
			where.append(" and partyType like ?");
			params.add("%%" + orgStructure.getPartyType() + "%%");
		}	
		
		where.append(" order by createDate desc");
		List<OrgStructure> list = orgStructureDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
}
